﻿#define _CRT_SECURE_NO_WARNINGS 1

#include <stdio.h>

//31.数组元素和与数字和的绝对差(库函数"abs"取绝对值)
#include <stdlib.h>
int differenceOfSum(int* nums, int numsSize) 
{
	int element_sum = 0, digit_sum = 0;
	int i = 0;
	for (i = 0; i < numsSize; i++)
	{
		element_sum += nums[i];  //元素和
		int tmp = nums[i];

		for (; tmp > 0; tmp /= 10)  //数字和
		{
			digit_sum += (tmp % 10);
		}
	}
	return abs(element_sum - digit_sum);
}


//32.回文数
#include<stdbool.h>
bool isPalindrome(int x) 
{
	if (x < 0)
	{
		return false;
	}
	long x1 = x, x2 = 0;
	while (x1)
	{
		x2 = x2 * 10 + x1 % 10;
		x1 /= 10;
	}
	return x == x2;
}


//33.有多少⼩于当前数字的数字
//解法1：(暴力枚举)
int* smallerNumbersThanCurrent(int* nums, int numsSize, int* returnSize) 
{
	int* ans = malloc(numsSize * sizeof(int)); //定义ans数组

	for (int i = 0; i < numsSize; i++)  //遍历数组
	{
		int count = 0;
		for (int j = 0; j < numsSize; j++)
		{
			if (nums[j] < nums[i])
			{
				count++;
			}
		}
		ans[i] = count;
	}

	*returnSize = numsSize;  //更新ans数组⻓度并返回ans数组
	return ans;
}
//解法2：(排序，二分查找)
int* smallerNumbersThanCurrent(int* nums, int numsSize, int* returnSize) 
{
	int* ans = malloc(numsSize * sizeof(int));    //定义答案数组
	int* order = malloc(numsSize * sizeof(int));  //定义有序数组

	for (int i = 0; i < numsSize; i++)    ////将原数组中的数放⼊有序数组，之后进⾏排序
	{
		order[i] = nums[i];
	}

	for (int i = 0; i < numsSize; i++)    //排序(冒泡)
	{
		for (int j = 0; j < numsSize - i - 1; j++) 
		{
			if (order[j] > order[j + 1]) 
			{
				int u = order[j];
				order[j] = order[j + 1];
				order[j + 1] = u;
			}
		}
	}

	//更新答案数组
	for (int i = 0; i < numsSize; i++) 
	{
		//将当前数在有序数组中的下标存⼊答案数组，order：有序数组，numsSize：数组⻓度，nums[]
		ans[i] = low(order, numsSize, nums[i]);
	}

	//更新数组⻓度并返回数组
	*returnSize = numsSize;
	return ans;
}


//34.字母在字符串中的百分比
#include <string.h>
int percentageLetter(char* s, char letter) 
{
	int count = 0;
	int len = strlen(s);
	while (*s != '\0')
	{
		if (letter == *s)
		{
			count++;
		}
		s++;
	}
	return count * 100 / len;
}

